<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Local\AvsData AS LocalAvsData;       //本地数据模型
use App\Models\Headquarters\AvsData AS HQAvsData;       //总部数据模型
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Log;

/**
 * 上报本地地磅数据到总部
 */
class AvsDataReportJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $timeout = 280; //队列超时时间
    public $failOnTimeout = true; //作业超时将其标记为失败
    protected $local_conn; //本地连接
    protected $remote_conn; //总部连接
    protected $local_data_table; //本地保存的原始数据集合
    protected $remote_data_table; //总部保存的数据集合

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($params=null)
    {
        $this->local_conn = $params && isset($params['local_conn']) ? $params['local_conn'] : '';
        $this->remote_conn = $params && isset($params['remote_conn']) ? $params['remote_conn'] : '';
        $this->local_data_table = $params && isset($params['local_data_table']) ? $params['local_data_table'] : '';
        $this->remote_data_table = $params && isset($params['remote_data_table']) ? $params['remote_data_table'] : '';
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $localAvsData = (new LocalAvsData())->setConnection($this->local_conn)->setTable($this->local_data_table);
        $hQAvsData = (new HQAvsData())->setConnection($this->remote_conn)->setTable($this->remote_data_table);
        $notReportDataList = $localAvsData->findNotReport();

        try {
            foreach($notReportDataList as $row){
                $param = array(
                    'Id' => $row->Id,
                    'RecordNo' => $row->RecordNo,
                    'FlowNo'=> $row->FlowNo,
                    'Facility'=> $row->Facility,
                    'Source'=> $row->Source,
                    'Driver'=> $row->Driver,
                    'VehicleNo'=> $row->VehicleNo,
                    'VehicleType'=> $row->VehicleType,
                    'TransDept'=> $row->TransDept,
                    'ShiftType'=> $row->ShiftType,
                    'ComputerName'=> $row->ComputerName,
                    'WeightChecker'=> $row->WeightChecker,
                    'InstrumentNo'=> $row->InstrumentNo,
                    'ImportStatus'=> $row->ImportStatus,
                    'WeightGross'=> $row->WeightGross,
                    'WeightTare'=> $row->WeightTare,
                    'WeightNet'=> $row->WeightNet,
                    'WeightDeduction'=> $row->WeightDeduction,
                    'BillingUnit'=> $row->BillingUnit,
                    'BillingTotal'=> $row->BillingTotal,
                    'TimeLoading'=> $row->TimeLoading,
                    'TimeWeightingG'=> $row->TimeWeightingG,
                    'TimeWeightingT'=> $row->TimeWeightingT,
                    'TimeWeightingN'=> $row->TimeWeightingN,
                    'Memo'=> $row->Memo,
                    'RecordStatus'=> $row->RecordStatus,
                    'RecordCreatedTime'=> $row->RecordCreatedTime,
                    'RecordCreatorId'=> $row->RecordCreatorId,
                    'FieldNo'=> $row->FieldNo,
                    'InputType'=> $row->InputType,
                    'GarbageType'=> $row->GarbageType,
                    'RecordVersion'=> $row->RecordVersion,
                    'VehicleDoorNo'=> $row->VehicleDoorNo,
                    'DepartmentSTA'=> $row->DepartmentSTA,
                    'TimePhoto'=> $row->TimePhoto,
                    'RfidTag'=> $row->RfidTag,
                    'Region'=> $row->Region
                );

                $obj = null;
                $isExist = $hQAvsData->findRowById($row->Id);
                if(!$isExist){
                    $obj = $hQAvsData->insertOne($param);
                }
                else{
                    $obj = true;
                }

                if($obj){
                    $localAvsData->updateById(["flag" => 1], $row->Id);
                }
            }
        } catch (QueryException $e) {
            Log::info('=========上报总部地磅数据失败=========');
            Log::info(var_export($e->getMessage(), true));
            DB::rollback();
        }
    }
}
